package controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import pojo.User;
import service.UserService;

@Controller

public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/login")
	public String userList() {
		return "login";

	}

	@RequestMapping("/logout")
	public String logout(HttpSession httpSession) {
		// httpSession.removeAttribute("loginUser");
		httpSession.invalidate();
		return "login";

	}

	@RequestMapping("/logincheck")
	public String login(User user, Model model, HttpSession httpSession) throws Exception {

		User loginUser = userService.login(user);

		if (loginUser != null) {
			httpSession.setAttribute("loginUser", loginUser);
			String userType = loginUser.getType();
			if ("admin".equals(userType)) {
				return "admin/main";
			} else if ("fangdong".equals(userType)) {
				return "fangdong/main";
			} else {
				return "zuke/main";
			}
		} else {
			String error = "error";
			model.addAttribute("error", error);
			return "login";
		}
	}

	@RequestMapping("/toindex")
	public String toindex(Model model) throws Exception {
		return "index";
	}
}
